package com.zqh.forum.services.impl;

import com.zqh.forum.common.AppResult;
import com.zqh.forum.common.ResultCode;
import com.zqh.forum.dao.BoardMapper;
import com.zqh.forum.exception.ApplicationException;
import com.zqh.forum.model.Board;
import com.zqh.forum.services.IBoardService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Autowired
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        //1.参数的非空校验
        if(num <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //2.抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用dao查询数据库中的信息
        List<Board> result = boardMapper.selectByNum(num);
        //3.返回结果
        return result;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }

        //2.查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            //1.打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + "board id:" + id);
            //2.抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //3.更新帖子数量
        //创建要更改的对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() + 1);
        //调用动态更新的方法
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //判断受影响的行数
        if(row != 1){
            log.warn(ResultCode.FAILED.toString() + "，受影响的行数不等于1。");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Board selectById(Long id) {
        if(id == null || id <= 0){
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //调用DAO查询数据
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //校验参数
        if(id == null || id <= 0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //根据板块id查询板块的信息
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null || board.getDeleteState() == 1){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //构建要修改的数据
        Board updateBoard = new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount() - 1);
        //判断-1之后的数字是不是小于0
        if(updateBoard.getArticleCount() < 0){
            //设置为0
            updateBoard.setArticleCount(0);
        }
        updateBoard.setUpdateTime(new Date());

        //调用Dao层
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row != 1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString() + "受影响的行数不等于1");
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }
}
